from sklearn.base import BaseEstimator, TransformerMixin, ClassifierMixin
import numpy
from collections import Counter
__author__ = 'panagiotis'


class ProbabilityTransformer(BaseEstimator, TransformerMixin):

    def __init__(self, classifier=None, **attrib):
        self.clf = classifier(**attrib)

    def fit(self, x, y):
        self.clf.fit(x, y)
        return self

    def transform(self, x):
        return self.clf.predict_proba(x)


class MeanClassifier(BaseEstimator, ClassifierMixin):

    def __init__(self, estimators=None):
        if estimators:
            self.estimators = estimators
        else:
            raise AttributeError

    def fit(self, X, y):
        for clf in self.estimators:
            clf.fit(X, y)

    def predict(self, X):
        return numpy.round(numpy.mean(numpy.array([clf.predict(X) for clf in self.estimators]), axis=0), 0)


class VotingClassifier(BaseEstimator, ClassifierMixin):

    def __init__(self, estimators=None):
        if estimators:
            self.estimators = estimators
        else:
            raise AttributeError

    def fit(self, X, y):
        for clf in self.estimators:
            clf.fit(X, y)

    def predict(self, X):
        result = []
        for item in zip(*[clf.predict(X) for clf in self.estimators]):
            counter = Counter()
            for score in item:
                counter[score] += 1
            result.append(counter.most_common(1)[0][1])
        return numpy.array(result)

